package com.arpa.wms.controller.wms;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.StrUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.dto.SplitOrderDTO;
import com.arpa.wms.domain.vo.DeliverySplitOrderVo;
import com.arpa.wms.domain.vo.DeliveryWithGoodsVO;
import com.arpa.wms.domain.vo.SplitOrderVO;
import com.arpa.wms.service.DeliveryCancelHandleService;
import com.arpa.wms.service.SplitOrderService;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 拆单
 * @author liuxr
 * @date 2021/12/23 16:31
 */
@Api(tags = "拆单")
@RestController
@RequestMapping(value = "/splitOrder", produces = "application/json;charset=UTF-8")
@Log4j2
public class SplitOrderController {

    @Resource
    public RedissonClient redissonClient;

    private final SplitOrderService splitOrderService;

    private final DeliveryCancelHandleService deliveryCancelHandleService;

    public SplitOrderController(SplitOrderService splitOrderService,
                                DeliveryCancelHandleService deliveryCancelHandleService) {
        this.splitOrderService = splitOrderService;
        this.deliveryCancelHandleService = deliveryCancelHandleService;
    }
    /**
     * 获取 订单拆分 列表
     *
     * @param splitOrderDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 订单拆分 列表", notes = "获取 订单拆分 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = SplitOrderVO.class))
    @NeedLogin
    @GetMapping
    public Result list(SplitOrderDTO splitOrderDTO, HttpServletRequest request) {
        try {
            SplitOrderVO brandVOSum = splitOrderService.queryListSum(splitOrderDTO);
            List<SplitOrderVO> brandVOList = splitOrderService.queryList(splitOrderDTO);

            Page returnPage = new Page(splitOrderDTO.getPageNum(),splitOrderDTO.getPageSize());
            returnPage.setRecords(brandVOList);
            returnPage.setTotal(brandVOSum.getTotal());
            returnPage.setSumObject(brandVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value = "获取订单详情", notes = "获取订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "code", value = "单号,订单号或者运单号", paramType = "path", dataType = "String",required = true)
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = DeliveryWithGoodsVO.class))
    @NeedLogin
    @GetMapping(value = "/detail/{code}")
    public Result detail(@PathVariable String code, HttpServletRequest request) {
        DeliverySplitOrderVo deliverySplitOrderVo = splitOrderService.getDeliverySplitOrderVoByWaybillCode(code);
        return Result.ok().data(deliverySplitOrderVo);
    }


    @ApiOperation(value = "拆分订单", notes = "拆分订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
    })
    @NeedLogin
    @PostMapping("/{code}")
    public Result batchGetWaybillCode(@PathVariable String code ,@RequestParam Integer splitPackageNum) {
        List<String> codes = new ArrayList<>();
        codes.add(code);
      /*  Set<String> cancelCodes = deliveryCancelHandleService.deliveryOrderCancel(codes);
        if(IterUtil.isNotEmpty(cancelCodes)){
            return Result.error("拆分失败，订单："+StrUtil.join(",",cancelCodes)+"已取消，请重新选择");
        }*/
        splitOrderService.split(code,splitPackageNum);
        return Result.ok("拆单成功");
    }

    /**
     * 打印拆单面单
     *
     * @param splitOrderDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "打印拆单面单", notes = "打印拆单面单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping("/printSplitExpress")
    public Result printExpress(SplitOrderDTO splitOrderDTO, HttpServletRequest request) {
        List<SplitOrderVO> deliveryVOList = splitOrderService.printSplitExpress(splitOrderDTO);
        return Result.ok().data(deliveryVOList);
    }

}
